package com.luojun.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.luojun.entity.Car;
import com.luojun.entity.Commodity;
import com.luojun.entity.User;
import com.luojun.service.CarService;
import com.luojun.service.UCommodityService;

/**
 * Servlet implementation class DoBuyOne
 */
@WebServlet("/DoBuyOne")
public class DoBuyOne extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoBuyOne() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UCommodityService ucs = new UCommodityService();
		int id = Integer.parseInt(request.getParameter("id"));
		User user =(User)session.getAttribute("user"); 
		Commodity commodity = ucs.findByCommodityId(id);
		CarService cs = new CarService();
		Car c = new Car();		
		c.setQuantity(0);
		c.setUser(user);
		c.setCommodity(commodity);
		cs.buy(c);
		ucs.buy(commodity);
		request.getRequestDispatcher("ToPersonalCenter").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
